<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ShopController extends Controller
{
    //商城点进去就是产品列表
    public  function index(Request $request)
    {
        $city_id  = request('city_id','');
     
   
       if(!empty($city_id)){
                         $list =  \DB::table('shop')
                            ->select('shop.pro_name','shop.id','shop.price','shop.img','type.id as tid','hos_list.id as h_id')
                            ->where('shop.is_del','=',0)
                            ->leftJoin('type','shop.type_id','=','type.id')
                            ->leftJoin('hos_list','shop.hos_id','=','hos_list.id')
                            ->get();
                        
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                             'data'=>$list,  
                    );
                    return $data;
           
       }else{
           
                      //如果城市ID不存在 那么进行随机查询
                      $list =  \DB::table('shop')
                              ->select('shop.pro_name','shop.id','shop.price','shop.img','type.id as tid','hos_list.id as h_id')
                            ->where('shop.is_del','=',0)
                            ->leftJoin('type','shop.type_id','=','type.id')
                            ->leftJoin('hos_list','shop.hos_id','=','hos_list.id')
                            ->get();
                      //$list = \DB::select("select type.* from type where city_id = $city_id order by concat(type.path,type.id) asc");
                      
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                         'data'=>$list,  
                    );
                    return $data;
                    
       }
       
    }
    
    //产品详情
    public  function shopinfo(Request $request)
    {
            
       $city_id  = request('city_id','');
                $id  = request('id','');     
       if(!empty($city_id)){
                         $list =  \DB::table('shop')
                            ->select('shop.id','shop.pro_name','shop.content','shop.img','shop.p_img','shop.content_text','shop.price','shop.xuzhi')     
                            ->where('shop.is_del','=',0)
                            ->where('id',$id)
                                 
                            ->leftJoin('type','shop.type_id','=','type.id')
                            ->leftJoin('hos_list','shop.hos_id','=','hos_list.id')
                            ->get();
                        
                           
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                             'data'=>$list,  
                    );
                    return $data;
           
       }else{
           
                      //如果城市ID不存在 那么进行随机查询
                      $list =  \DB::table('shop')
                             ->select('shop.id','shop.pro_name','shop.content','shop.img','shop.p_img','shop.content_text','shop.price','shop.xuzhi') 
                            ->where('shop.is_del','=',0)
                            ->where('id',$id)
                            ->leftJoin('type','shop.type_id','=','type.id')
                            ->leftJoin('hos_list','shop.hos_id','=','hos_list.id')
                            ->get();
                      //$list = \DB::select("select type.* from type where city_id = $city_id order by concat(type.path,type.id) asc");
                      
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                         'data'=>$list,  
                    );
                    return $data;
                    
       }
        
        
    }
}
